package com.shine.icis.config;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Element;

import com.shine.cotrun.web.upload.UploadConfig;
import com.shine.cotrun.xml.XMLReader;

/**
 * com.shine.oa.syscfg.SysRuntimeParams.java
 * 系统启动运行参数类
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: SHINETECH</p>
 * <p>Company: SHINETECH</p>
 * @author zsy
 * @version 
 */
public class SysRuntimeParams extends XMLReader {
	private static Log log = LogFactory.getLog(SysRuntimeParams.class);
	public static final String CONFIG_FILE_NAME = "/configuration.xml";

	private static SysRuntimeParams single = null;
	
	private UploadConfig uploadConfig;//更改上传文件路径相关配置

	private Map<String, String> params;//配置参数

	/**
	 * System runtime parameters loader
	 * @return the parameters set , by SysRuntimeParams's singleton instance
	 */
	public static SysRuntimeParams loadSysRuntimeParams(){
		if(single == null){
			log.info("载入系统配置文件..");
			InputStream is = null;
			try {
				is = SysRuntimeParams.class.getResourceAsStream(CONFIG_FILE_NAME);
				single = new SysRuntimeParams(is);
			} catch (Exception e) {
				log.error("系统配置文件：" + CONFIG_FILE_NAME + "缺失，系统将不能正常运行！", e);
				return null;
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			
		}
		return single;
	}
	
	private SysRuntimeParams(InputStream stream) {
		super(stream);
	}
	
	/**
	 * implement the inteface "XMLParseTemplate" 
	 */
	@SuppressWarnings("unchecked")
	public void doRead() {
		Element root = document.getRootElement();
		String temp = null;
		//读取上传附件的配置
		Element ele_atta = root.element("attachment");
		if (ele_atta != null) {
			uploadConfig = new UploadConfig();
			
			Element ele_up = ele_atta.element("upload-config");
			if (ele_up != null) {
				uploadConfig.setRootFolder(ele_up.elementText("root-folder"));
				uploadConfig.setTempFolder(ele_up.elementText("temp-folder"));
				uploadConfig.setImgExt(ele_up.elementText("img-ext"));
			}
			
			ele_up = ele_atta.element("upload-paths");
			if (ele_up != null) {
				temp = ele_up.attributeValue("enable");
				uploadConfig.setEnableUploadPath("true".equalsIgnoreCase(temp));
				
				for (Element e : (List<Element>)ele_up.elements("path")) {
					temp = e.getText();
					if (temp == null || temp.trim().equals("")) {
						continue;
					}
					if (uploadConfig.getSavePath() == null && 
							"true".equalsIgnoreCase(e.attributeValue("save"))) {
						uploadConfig.setSavePath(temp);
					} else {
						uploadConfig.addFindPath(temp);
					}
				}
			}
			
			ele_up = ele_atta.element("referer-check");
			if (ele_up != null) {
				temp = ele_up.attributeValue("enable");
				uploadConfig.setEnableRefererCheck("true".equalsIgnoreCase(temp));
				uploadConfig.setAccreditHosts(ele_up.getTextTrim());
			}
		}
		
		//读取
		params = new HashMap<String, String>();
		Element ele_params = root.element("parameters");
		List<Element> pList = ele_params.elements("parameter");
		for (Element pe : pList) {
			String name = pe.attributeValue("name");
			if (name != null) {
				params.put(name, pe.getText());
			}
		}
	}
	
	/**
	 * 取得附件配置
	 * @return
	 */
	public UploadConfig getUploadConfig() {
		return uploadConfig;
	}
}